﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows.Input;


namespace MoneyManagerNET.Presentation.ViewModel
{
    using Model;
    using Infrastructure;
    using MoneyManagerNET.Presentation.ViewModel.Base;

    public class AssetListViewModel:ListViewModel<IAsset>//ViewModelBase<IAsset>
    {
        public AssetListViewModel()
            : base(App.Storage.Select<IAsset>())
        {
            _newCommand = new DelegateCommand(o=>New());
            _editCommand = new DelegateCommand(o=>Edit());
        }


        public decimal Total {
            get {
                return Items.Sum(a=>a.GetCurrentValue());
            }
        }

        public void New() {
            var asset=App.Storage.New<IAsset>();

            var vm = new AssetViewModel();
            vm.Model = asset;

            var result = vm.ShowDialog();
            if (result.HasValue && result.Value)
            {
                this.Items.Add(asset);
            }
            else {
                App.Storage.Delete(asset);
            }
        }


        DelegateCommand _newCommand;
        public ICommand NewCommand { get { return _newCommand; } }

        public void Edit(){
            var asset = Selected;
            if (asset == null) return;

            var vm = new AssetViewModel();
            vm.Model = asset;

            var result = vm.ShowDialog();
            if (result.HasValue && result.Value)
            {
                //this.Assets.Add(asset);
            }
            else
            {
            }
        }

        DelegateCommand _editCommand;
        public ICommand EditCommand { get { return _editCommand; } }

        

        

        public string ChangeText { get { return (Selected == null) ? string.Empty : "Change in Value: " + (Selected.ChangeInValue == 0 ? "Appreciates" : "Depreciates") + "=" + Selected.Rate + "%"; } }

    }
}
